<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>入库记录</title>
    <script type="text/javascript">
      var newWarehousingWin;
      var newClaimWin;
      var editWarehousingEntryOrderWin;
      var warehousingEntryId;
      var warehousingEntryDS;
      
      Ext.onReady(function(){
        var vendorTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{number}|{name}', '</div></tpl>');
        var vendorStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/vendor/getAllVendors?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }])
        });

        var purchaseOrderNumber = new Ext.form.TextField({
          fieldLabel: '采购单号',
          name: 'purchase_order_number',
          anchor: '95%'
        });

        var orderNumber = new Ext.form.TextField({
          fieldLabel: '入库单号',
          anchor: '95%',
          name: 'order_number'
        });

        var vendor =  new Ext.form.ComboBox({
          fieldLabel: '供应商',
          store: vendorStore,
          valueField: 'id',
          displayField: 'name',
          hiddenName:'vendor_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          //pageSize: 10,
          //hideTrigger: false,
          tpl: vendorTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction: 'all',
          editable:false,
          anchor: '95%',
          selectOnFocus:true
        });

        var beginDate = new Ext.form.DateField({
          fieldLabel: '入库日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          endDateField: 'end_date'
        });
        
        var endDate = new Ext.form.DateField({
          fieldLabel: '至    ',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          startDateField: 'begin_date'
        });

        var productTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">','<div style="float:left; width:220px">{name}</div><div style="float:left; width:90px">{number}</div>{is_suit}','</div></tpl>');
        productTemplate.compile();
        var productStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/product/getAllProducts?for_cbb=true_simple"
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }, {
              name: 'is_suit',
              mapping:'is_suit',
              convert:function(value,record){
                if(value == "1"){
                  return "套装";
                }else if(value == "0"){
                  return "单品";
                }
              }
            }])
        });
        //产品
        var productCBB = new Ext.form.ComboBox({
          store: productStore,
          fieldLabel: '产品',
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'product_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '100%',
          pageSize: 25,
          //hideTrigger: false,
          tpl: productTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction: 'all',
          selectOnFocus:true
        });
        
        var submitButton = new Ext.Button({
          text: '搜索',
          iconCls: 'finding',
          handler: function(){
            warehousingEntryDS.removeAll();
            warehousingEntryDS.baseParams.order_number = orderNumber.getValue();
            warehousingEntryDS.baseParams.purchase_order_number = purchaseOrderNumber.getValue();

            warehousingEntryDS.baseParams.begin_date = beginDate.getRawValue();
            warehousingEntryDS.baseParams.end_date = endDate.getRawValue();
            warehousingEntryDS.baseParams.vendor_id = vendor.getValue();
            warehousingEntryDS.baseParams.product_id = productCBB.getValue();
            warehousingEntryDS.load();
          }
        });

        var clearButton = new Ext.Button({
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          //renderTo: 'query_form',
          labelWidth: 65,
          width: screenWidth-175,
          labelAlign: 'right',
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 1100},
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .17,
                      layout: 'form',
                      border: false,
                      items: [purchaseOrderNumber]
                    }, {
                      columnWidth: .17,
                      layout: 'form',
                      border: false,
                      items: [orderNumber]
                    }, {
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [vendor]
                    }]
                },{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .17,
                      layout: 'form',
                      border: false,
                      items: [beginDate]
                    }, {
                      columnWidth: .17,
                      layout: 'form',
                      border: false,
                      items: [endDate]
                    },{
                      columnWidth: .38,
                      layout: 'form',
                      border: false,
                      items: [productCBB]
                    }, {
                      columnWidth: .13,
                      layout: 'form',
                      border: false,
                      items: [submitButton]
                    }, {
                      columnWidth: .13,
                      layout: 'form',
                      border: false,
                      items: [clearButton]
                    }]
                }]
            }]
        });
        //显示入库操作时的窗口介面。
        //----------------------------------------------------------------------
        //入库Grid Store
        warehousingEntryDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/warehousing_entry/getAllWarehousingEntries'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'purchase_order_number',
              mapping: 'purchase_order',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.order_number;
                }
              }
            },{
              name: 'purchase_order_status',
              mapping: 'purchase_order',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.auditing_status;
                }
              }
            },{
              name: 'order_number'
            },{
              name: 'auditing_status'
            }, {
              name: 'action_date'
            }, {
              name: 'description'
            }, {
              name: 'employee_name',
              mapping:'employee.name'
            },{
              name: 'department_name',
              mapping:'department.name'
            }, {
              name: 'warehouse_name',
              mapping:'warehouse.name'
            }, {
              name: 'vendor_name',
              mapping:'vendor.name'
            }, {
              name: 'description'
            },{
              name: 'deal_with_status'
            }, {
              name: 'created_at'
            }, {
              name: 'updated_at'
            }])
        });
        
        //生成入库Grid的CM
        var warehousingEntrySM = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
        var warehousingEntryCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),warehousingEntrySM,{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '采购单号',
              dataIndex: 'purchase_order_number',
              width: 130
            },{
              header: '采购订单审批状态',
              dataIndex: 'purchase_order_status',
              width: 110,
              renderer: auditingStatusRender
            },{
              header: '入库单号',
              dataIndex: 'order_number',
              width: 120
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 80,
              renderer: auditingStatusRender
            },{
              header: '供应商',
              dataIndex: 'vendor_name',
              width: 190
            },{
              header: '到仓库',
              dataIndex: 'warehouse_name',
              width: 80
            },{
              header: '入库日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 75
            },{
              header: '操作员',
              dataIndex: 'employee_name',
              width: 70
            },{
              header: '部门',
              dataIndex: 'department_name',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 130
            },{
              header: '入库单状态',
              dataIndex: 'deal_with_status',
              width: 85,
              renderer: auditingStatusRender
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 110
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 110
            }
          ]});

        //生成要订单的产品的Grid
        var warehousingEntryGrid = new Ext.grid.GridPanel({
          title:'入库单列表',
          store: warehousingEntryDS,
          cm: warehousingEntryCM,
          sm: warehousingEntrySM,
          loadMask: true,
          width: screenWidth-175,
          height: 345,
          frame: true,
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: warehousingEntryDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: [{
              text: '新增入库单',
              disabled:true,
              iconCls: 'addItem',
              handler : function(){
                newWarehousingWin = new Ext.Window({
                  layout: 'fit',
                  width: 810,
                  height: 500,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加入库单',
                  autoLoad: {
                    url: "/warehousing_entry/newEntry",
                    scripts: true
                  }
                });
                newWarehousingWin.on("destroy",function(){
                  warehousingEntryDS.reload();
                });
                newWarehousingWin.resizable = false;
                newWarehousingWin.show();
              }
            },'-',{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = warehousingEntryGrid.getSelectionModel().getSelections();
                if (records.length == 0) {
                  return false;
                }else{
                  warehousingEntryId = records[0].get('id');
                  if(records[0].get('auditing_status').indexOf('通过新增') > -1 || records[0].get('auditing_status').indexOf('通过变更') > -1 || records[0].get('auditing_status').indexOf('驳回新增') > -1 || records[0].get('auditing_status').indexOf('驳回撤消') > -1){
                    editWarehousingEntryOrderWin = new Ext.Window({
                      layout: 'fit',
                      width: 917,
                      height: 483,
                      closeAction: 'destroy',
                      plain: true,
                      modal: true,
                      constrain:true,
                      //modal: true,
                      title: '编辑入库单',
                      autoLoad: {
                        url: "/warehousing_entry/editEntryFromPurchaseOrder?id="+warehousingEntryId,
                        scripts: true
                      }
                    });
                    editWarehousingEntryOrderWin.on("destroy",function(){
                      warehousingEntryDS.reload();
                    });
                    editWarehousingEntryOrderWin.resizable = false;
                    editWarehousingEntryOrderWin.show();
                  }
                }
              }
            },'-',{
              text: '撤消',
              iconCls: 'cancel',
              handler : function(){
                var records = warehousingEntryGrid.getSelectionModel().getSelections();
                if (records.length == 0) {
                  return false;
                }else{
                  if(records[0].get('deal_with_status') != '已撤消'){
                    var orderNumber = records[0].get('order_number')
                    warehousingEntryId = records[0].get('id');
                    Ext.MessageBox.confirm("提示","你确认要申请撤消订单号为： \""+orderNumber+"\" 的入库单吗？",function(btn){
                      if(btn=="yes"){
                        Ext.Ajax.request({
                          url : '/warehousing_entry/applyToCancel?id=' + warehousingEntryId,
                          success : function (result, request) {
                            warehousingEntryDS.reload();
                          },
                          failure : function (result, request){
                            Ext.MessageBox.show({
                              title: '消息',
                              msg: "通讯失败，请从新操作",
                              buttons: Ext.MessageBox.OK,
                              icon: Ext.MessageBox.WARNING
                            });
                          },
                          method : 'GET'
                        });
                      }else{
                        //alert("no");
                      }
                    });
                  }
                }
              }
            },'-',{
              text: '打印入库单',
              iconCls: 'print',
              handler : function(){
                var records = warehousingEntryGrid.getSelectionModel().getSelections();
                if (records.length == 0) {
                  return false;
                }else{
                  if(records[0].get('deal_with_status') != '已撤消'){
                    warehousingEntryId = records[0].get('id');
                    //LODOP.SET_PRINT_PAGESIZE(1,0,0,'A5')
                    LODOP.PRINT_INIT("");
                    //A4
                    LODOP.SET_PRINT_PAGESIZE(1,"210mm","285mm","")
                    //A5
                    //LODOP.SET_PRINT_PAGESIZE(1,"210mm","148mm","")
                    //LODOP.SET_PRINT_PAGESIZE(1,"148mm","210mm","")
                    LODOP.ADD_PRINT_URL(30,'5%',"90%","100%",'http://'+window.location.host+'/warehousing_entry/sale_entry_print_header?sub_type=2&id='+warehousingEntryId);
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
                    //LODOP.ADD_PRINT_TBURL(170,'5%',"90%","43%",'http://'+window.location.host+'/warehousing_entry/sale_entry_print_content?sub_type=2&id='+warehousingEntryId);
                    LODOP.ADD_PRINT_TBURL(170,"5%","90%","70%",'http://'+window.location.host+'/warehousing_entry/sale_entry_print_content?sub_type=2&id='+warehousingEntryId);
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",4);
                    //LODOP.ADD_PRINT_URL(425,'5%',"90%","100%",'http://'+window.location.host+'/warehousing_entry/sale_entry_print_footer?sub_type=2&id='+warehousingEntryId);
                    LODOP.ADD_PRINT_URL("85%",'5%',"90%","100%",'http://'+window.location.host+'/warehousing_entry/sale_entry_print_footer?sub_type=2&id='+warehousingEntryId);
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
                    //LODOP.ADD_PRINT_TEXT(495,'15%',"80%","8%","第一联：成本（白） 第二联：财务（绿） 第三联：提货（粉） 第四联：客户（黄） 第五联：销售（兰）");
                    LODOP.ADD_PRINT_TEXT("93%",'15%',"80%","8%","第一联：成本（白） 第二联：财务（绿） 第三联：提货（粉） 第四联：客户（黄） 第五联：销售（兰）");
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
                    //LODOP.ADD_PRINT_TEXT(515,'35%',"90%","8%","页号：第#页/共&页 打印日期："+getDate());
                    LODOP.ADD_PRINT_TEXT("95%",'35%',"60%","8%","页号：第#页/共&页 打印日期："+getDate());
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",2);
                    //LODOP.PREVIEW();
                    LODOP.PRINT_DESIGN();
                    //window.open('/test/sale_order_print?id='+saleOrderId,'_blank','width=0,height=0,toolbar=no,menubar=no,location=no');
                  }
                }
              }
            }]
        });
        warehousingEntryDS.load();

        warehousingEntryGrid.on('rowdblclick', function(gridPanel, rowIndex, e){
          warehousingEntryId = warehousingEntryDS.getAt(rowIndex).get('id');
          var warehousingEntryNumber = warehousingEntryDS.getAt(rowIndex).get('order_number');
          //warehousingEntryDS.reload();
          warehousingEntryDetailDS.removeAll();
          warehousingEntryDetailDS.baseParams.id = warehousingEntryId;
          warehousingEntryDetailDS.load();
          warehousingEntryDetailGrid.setTitle("采购订单："+warehousingEntryNumber+"，的详细信息");
        });

        warehousingEntryGrid.on('rowcontextmenu', function(grid, rowIndex, e){
          warehousingEntryId = warehousingEntryDS.getAt(rowIndex).get('id');
          currentOrderNumber = warehousingEntryDS.getAt(rowIndex).get('order_number');
          var auditingStatus = warehousingEntryDS.getAt(rowIndex).get('auditing_status');
          transferIntoClaim.setDisabled(true);
          if (auditingStatus.indexOf("撤消") < 0 && auditingStatus.indexOf("申请") < 0){
            transferIntoClaim.setDisabled(false);
          }
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });

        var transferIntoClaim = new Ext.menu.Item({
          text: '【转为索赔】',
          //iconCls: 'album',
          handler: rightMenuTransferIntoClaimFn
        });

        function rightMenuTransferIntoClaimFn(){
          newClaimWin = new Ext.Window({
            layout: 'fit',
            width: 917,
            height: 483,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '从入库单：\"'+currentOrderNumber+'\"中提取索赔信息',
            autoLoad: {
              url: "/claim/newClaimFromEntry?id="+warehousingEntryId,
              scripts: true
            }
          });
          newClaimWin.on("destroy",function(){
            warehousingEntryDS.reload();
          });
          newClaimWin.resizable = false;
          newClaimWin.show();
        }

        var entryHxItem = new Ext.menu.Item({
          text: '【变更历史】',
          //iconCls: 'album',
          handler: rightMenuEntryHxFn
        });

        function rightMenuEntryHxFn(){
          entryHxWin = new Ext.Window({
            layout: 'fit',
            width: 960,
            height: 550,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '入库单：\"'+currentOrderNumber+'\"的变更记录',
            autoLoad: {
              url: "/warehousing_entry/hxListForEntry?id="+warehousingEntryId,
              scripts: true
            }
          });
          entryHxWin.on("destroy",function(){
            warehousingEntryDS.reload();
          });
          entryHxWin.resizable = false;
          entryHxWin.show();
        }
        
        var rightClick = new Ext.menu.Menu({
          items: [transferIntoClaim,'-',entryHxItem]
        });
        
        //--------------------------采购订单详细，双击后可显示数据----------------
        //采购订单Grid Store Detail
        var warehousingEntryDetailDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/warehousing_entry/getWarehousingEntryDetailsById'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'sale_order_number',
              mapping:'sale_order',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.order_number;
                }
              }
            },{
              name: 'product_name',
              mapping:'product.name'
            },{
              name: 'product_number',
              mapping:'product.number'
            },{
              name: 'product_spec',
              mapping:'product.spec'
            },{
              name: 'quantity',
              type: 'int'
            },{
              name: 'is_suit',
              mapping:'product.is_suit',
              convert:function(value,record){
                if(value == "1"){
                  return "套装";
                }else if(value == "0"){
                  return "单品";
                }
              }
            }, {
              name: 'description'
            }])
        });

        //生成采购订单详细表的Grid的CM
        var warehousingEntryDetailSM = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
        var warehousingEntryDetailCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),warehousingEntryDetailSM,{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 60
            },{
              header: '销售订单单号',
              dataIndex: 'sale_order_number',
              width: 170
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 230
            },{
              header: '产品编号',
              dataIndex: 'product_number',
              width: 80
            },{
              header: '规格',
              dataIndex: 'product_spec',
              width: 80
            },{
              header: '数量',
              align: 'right',
              dataIndex: 'quantity',
              width: 80
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 150
            }
          ]});
        //生成要订单的产品的Grid
        var warehousingEntryDetailGrid = new Ext.grid.GridPanel({
          title:'入库单详细列表',
          store: warehousingEntryDetailDS,
          cm: warehousingEntryDetailCM,
          sm: warehousingEntryDetailSM,
          loadMask: true,
          width: screenWidth-175,
          height: screenHeight-660,
          frame: true
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
        });

        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-175,
          height: screenHeight-200,
          frame:false,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          items:[queryFormPanel,warehousingEntryGrid,warehousingEntryDetailGrid]
        });
        workbenchPanel.render("warehousing_entry_list");
        
      })
    </script>
  </head>
  <body>
    <div id="warehousing_entry_list"></div>
  </body>
</html>
